 // 水深随机数
const startTime = new Date()         //  采集数据起始时间
var depth = 27                        //   水深
var depthRandom = 0
var EchoData = []
var EchoFunc = {                     // FUNC 按钮选项
  option: {},
  selection: {}
}
  // 划线
function drawLine (x1, y1, x2, y2, color, cxt) {
  cxt.beginPath()
  cxt.lineWidth = 2
  cxt.strokeStyle = color
  cxt.moveTo(x1, y1)
  cxt.lineTo(x2, y2)
  cxt.stroke()
}
  //  画点
function drawDot (x, y, r, color, cxt) {
  cxt.fillStyle = color
  cxt.beginPath()
  cxt.arc(x, y, r, 0, Math.PI * 2, true)
  cxt.fill()
}
  //  画矩形
function drawRect (x1, y1, width, height, color, cxt) {
  cxt.fillStyle = color
  cxt.beginPath()
  cxt.fillRect(x1, y1, width, height)
  cxt.stroke()
}

  //  画y轴 水深刻度
function drawScale (ctx, _width, color) {
  drawLine(_width, 0, _width, 650, color, ctx)
  let unit = 30   //  刻度区间
  let y = 0
  for (let i = 0; i < 20; i++) {
    y += unit
    drawLine(_width, y, _width - 20, y, color, ctx)
  }
  y = 30
  unit *= 5
  for (let i = 0; i < 4; i++) {
    drawLine(_width, y, _width - 40, y, color, ctx)
    y += unit
  }
}
  //  画x轴 时间刻度
function drawTimeScale (ctx, width, time) {
  //  将毫秒换算成分钟
  //  1 min = 60000ms
  let msec = time.getTime() - startTime.getTime()
  let m = msec / 60000
  m = Math.floor(m)
  if (m % 10 === 0) {
    drawRect(width, 0, 2, 10, '#66C7C6', ctx)
  } else if (m % 2 === 0) {
    drawRect(width, 0, 2, 10, '#B7B8B9', ctx)
  }
}

  //  画出水深警戒线
function drawAlarmLine (ctx, width, maxRange, alarmDepth) {
  if (alarmDepth > maxRange) {
    return
  }
  let y = alarmDepth * 605 / maxRange
  y += 30
  drawLine(10, y, width, y, '#9F242B', ctx)
}

  //  画 disp 读到水深数据图形
function drawEcho (ctx, echoData, width, maxRange) {
  for (let i = echoData.length - 1; i > 0; i--) {
    let _data = echoData[i].depth

    //  画时间轴刻度线
    drawTimeScale(ctx, width, echoData[i].time)

    if (_data < maxRange && _data !== 0) {
      // 0 刻度线是从 y = 30 开始的
      let y = 30
      y += _data * 605 / maxRange
      //  画回波线条
      drawEchoLine(width, y, echoData[i].seed,
        echoData[i].gain, ctx)
      //  画杂波
      // drawClutter(width, maxRange, echoData[i], ctx)
    }

    //  根据PICT 判断移动距离
    if (echoData[i].PICT === 'FAST') {
      let tmp = (maxRange - _data) / maxRange
      if (tmp <= 1 / 4) {
        width -= 1.5
      } else {
        width -= 0.5
      }
    } else {
      width -= 0.5
    }

    if (width <= 0) {
      break
    }
  }
}

function HSB_2_RGB (H, S, B) {
  let rgb = {
    R: 0,
    G: 0,
    B: 0
  }
  H = (H >= 360) ? 0 : H
  if (S === 0) {
    rgb.R = B * 255
    rgb.G = B * 255
    rgb.B = B * 255
  } else {
    let i = Math.floor(H / 60) % 6
    let f = H / 60 - i
    let p = B * (1 - S)
    let q = B * (1 - S * f)
    let t = B * (1 - S * (1 - f))

    switch (i) {
      case 0:
        rgb.R = B
        rgb.G = t
        rgb.B = p
        break
      case 1:
        rgb.R = q
        rgb.G = B
        rgb.B = p
        break
      case 2:
        rgb.R = p
        rgb.G = B
        rgb.B = t
        break
      case 3:
        rgb.R = p
        rgb.G = q
        rgb.B = B
        break
      case 4:
        rgb.R = t
        rgb.G = p
        rgb.B = B
        break
      case 5:
        rgb.R = B
        rgb.G = p
        rgb.B = q
        break
    }
    rgb.R = rgb.R * 255
    rgb.G = rgb.G * 255
    rgb.B = rgb.B * 255
  }
  return rgb
}

let hsb = {
  H: 57.8761,
  S: 0.4669,
  B: 0.9490
}

function getRgba (h, s, b, a) {
  let rgb = HSB_2_RGB(h, s, b)
  return 'rgba(' + Math.round(rgb.R) + ',' + Math.round(rgb.G) +
    ',' + Math.round(rgb.B) + ',' + a + ')'
}

//  画回波线条
function drawEchoLine (x, y, seed, gain, ctx) {
   // maxj 是回波的长度
  Random.seed = seed
  let maxj = 5 + 9 * 4 + Random.seededRandom() * 30

  let h = hsb.H
  let s = hsb.S
  let b = hsb.B

  //  for (let j = 0; j < maxj; j++) 需要优化
  for (let j = 0; j < 1; j++) {
    let a = 1.0
    // 30值是手动调试出来的
    if (j >= 30) {
      a = (maxj - j) / (maxj - 30)
    }
    // 保持10个长度的深颜色带
    if (j < 10) {
      s = 0.5
    } else {
      s = 0.5 - 0.3 * j / (maxj)
    }
    // s = 0.4
    a = 1.0 - j / (maxj - 10)
    a = Math.min(a, 1.0)
    // s = hsb.S + (1.0 - hsb.S) / maxj * j
    drawDot(x, j + y, 2, getRgba(h, s, b, a), ctx)
  }
}

  //  画杂波
// function drawClutter (x, maxRange, data, ctx) {
//   let num = data.clutterNum
//   let pos = data.clutterPos
//   let gain = data.gain
//   let seed = data.seed
//   let color = 'rgba(242,238,129,alp)'
//   for (let i = 0; i < num; i++) {
//     Random.seed = seed
//     let s = Random.seededRandom(gain / 10.0, 1.0)
//     let _depth = pos[i]
//     let y = 30
//     y += _depth * 605 / maxRange
//     let _color = color.replace('alp', s)
//     drawDot(x, y, 2, _color, ctx)
//   }
// }
  //  获取随机数
var Random = {
  seed: 13,
  seededRandom: function (min, max) {
    max = max || 1
    min = min || 0
    this.seed = (this.seed * 9301 + 49297) % 233280
    let rnd = this.seed / 233280.0
    rnd = min + rnd * (max - min)
    return rnd
  },
  getSeed: function (min, max) {
    let range = max - min
    let rand = Math.random()
    rand = min + rand * range
    return rand
  }
}

  //  获取历史数据
function getHistoryData (echoData, maxRange, interval, historyLog) {
  // 记录当前时间
  historyLog._time = new Date()
  historyLog._data = []
  // 判断是隔多少时间取数据， echoData保存的是每秒数据
  // 所以interval 要转成隔多少秒取数据
  if (interval.indexOf('5s') !== -1) {
    interval = 5
  } else if (interval.indexOf('1 min') !== -1) {
    interval = 60
  } else if (interval.indexOf('2 min') !== -1) {
    interval = 120
  } else {
    interval = 1
  }
  for (let i = echoData.length - 1; i >= 0; i -= interval) {
    let tmp = echoData[i].depth
    if (tmp < maxRange) {
      historyLog._data.push(tmp)
    }
  }
}

  //  画出历史线
function drawHistoryLine (ctx, width, maxRange, historyLog) {
  // 将历史坐标x轴分成720份
  // 每份 间隔 x 像素
  let x = width / 720
  ctx.beginPath()
  ctx.lineWidth = 2
  ctx.strokeStyle = '#3E98BF'
  let _data = historyLog._data
  for (let i = 0; i < _data.length; i++) {
    let tmp = _data[i]
    if (tmp < maxRange) {
      let y = tmp * 605 / maxRange
      y += 30
      if (i === 0) {
        ctx.moveTo(width, y)
      } else {
        ctx.lineTo(width, y)
      }
    }
    width -= x
    if (width <= 0) {
      break
    }
  }
  ctx.stroke()
}

  // 画历史记录时间轴
function drawTimeAxis (ctx, timePos) {
  drawLine(timePos, 0, timePos, 650, '#AB7143', ctx)
}

function clearEcho (ctx, width, height) {
  ctx.clearRect(0, 0, width, height)
}

function checkTime (tmp) {
  if (tmp < 10) {
    return '0' + tmp
  }
  return tmp
}
function getLocalTime () {
  let _date = new Date()
  let h = _date.getHours()
  let m = _date.getMinutes()
  let s = _date.getSeconds()
  h = checkTime(h)
  m = checkTime(m)
  s = checkTime(s)
  return h + ':' + m + '\'' + s
}

function getDate () {
  let _date = new Date()
  let arr = _date.toDateString().split(' ')
  let day = parseInt(arr[2])
  day = checkTime(day)
  return day + '/' + arr[1] + '/' + arr[3]
}
// let _externalTime = EchoMenu.getMenuItems('menu/System/Ship\'s Time/External')
// function getUTCTime () {
//   let date = new Date()
//   let offset = _externalTime[2].value
//   let isPlus = offset.indexOf('+')
//   let _h = offset.substr(1, 2)
//   let _m = offset.substr(4, 5)
//   _h = parseInt(_h)
//   _m = parseInt(_m)
//
//   let h = date.getUTCHours()
//   let m = date.getUTCMinutes()
//
//   if (isPlus !== -1) {
//     h += _h
//     m += _m
//     if (m >= 60) {
//       h += 1
//       m = 0
//     }
//     if (h >= 24) {
//       h = 0
//     }
//   } else {
//     h -= _h
//     m -= _m
//     if (m < 0) {
//       m += 60
//       h -= 1
//     }
//     if (h < 0) {
//       h -= 24
//     }
//   }
//   h = checkTime(h)
//   m = checkTime(m)
//   return h + ':' + m
// }

 // 计算得到对应经纬度
function getLatLonString (LatLon, type) {
  let unit = ''
  if (type === 'lat') {
    LatLon >= 0 ? unit = 'N' : unit = 'S'
  } else if (type === 'lon') {
    LatLon >= 0 ? unit = 'E' : unit = 'W'
  }
  let d = parseInt(LatLon)
  let m = LatLon - d
  m *= 60

  let strD = d.toFixed() + '°'

  let strM
  m >= 10 ? strM = m.toFixed(3) + '′' : strM = '0' + m.toFixed(3) + '′'
  return strD + strM + unit
}

export default {
  depthRandom,
  depth,
  EchoData,
  EchoFunc,
  Random,
  drawScale,
  clearEcho,
  drawAlarmLine,
  drawTimeScale,
  drawEcho,
  getHistoryData,
  drawHistoryLine,
  drawTimeAxis,
  getLocalTime,
  getDate,
  getLatLonString
}
